<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Card extends Model
{
    protected $table = 'mckj_card';

    protected $fillable = [
        'name', 'price', 'number', 'status', 'validity_day', 'image_url', 'careful_item', 'mcstation_ids',
    ];

    const CREATED_AT  = 'created_time';

    const UPDATED_AT = 'updated_time';

    const STATUS_VALID = 1;

    const STATUS_INVALID = 0;

    public static $statusText = [
        self::STATUS_VALID => '有效',
        self::STATUS_INVALID => '无效',
    ];

    public function getStatusTextAttribute()
    {
        return self::$statusText[$this->status] ?? '';
    }

    public function getNumberHumanAttribute()
    {
        if ($this->number == -1) {
            return '无限次';
        }
        return $this->number . "次";
    }

    public function getValidityDayHumanAttribute()
    {
        if ($this->validity_day == -1) {
            return '永久有效';
        }
        return $this->validity_day . "天";
    }

    public function getMcstationIdsAttribute($value)
    {
        return explode(',', $value);
    }

    public function setMcstationIdsAttribute($value)
    {
        $this->attributes['mcstation_ids'] = implode(',', $value);
    }

}
